package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewOthersPersonalActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.personalcenter.SweepFlowerBean;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.ui.view.ImageUtil;
import com.taptech.doufu.ui.view.RoundImageView;

import java.util.ArrayList;
import java.util.List;

public class HorizontalListViewAdapter extends BaseAdapter {

    private List<SweepFlowerBean> flowerInfo;

    private Context mContext;
    private LayoutInflater mInflater;
    private int selectIndex = -1;

    private RoundImageView imageView;
    private Bitmap bitmap;
    private Handler handler;
    private int type = 0;
    private boolean is_text_show = true;
    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_CP_LIST = 1;
    public static final int TYPE_CP_DES = 2;

    public HorizontalListViewAdapter(Context context, List<SweepFlowerBean> flowerInfo) {
        this.mContext = context;
        this.flowerInfo = flowerInfo;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// LayoutInflater.from(mContext);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                switch (msg.what) {
                    case 1:
                        imageView.setImageBitmap(bitmap);
                        break;

                    default:
                        break;
                }
                super.handleMessage(msg);
            }
        };

    }

    public HorizontalListViewAdapter(Context context, List<SweepFlowerBean> flowerInfo, int type, boolean is_text_show) {
        this.mContext = context;
        this.flowerInfo = flowerInfo;
        this.type = type;
        this.is_text_show = is_text_show;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// LayoutInflater.from(mContext);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                switch (msg.what) {
                    case 1:
                        imageView.setImageBitmap(bitmap);
                        break;

                    default:
                        break;
                }
                super.handleMessage(msg);
            }
        };

    }

    public HorizontalListViewAdapter(Context context) {
        this.mContext = context;
        flowerInfo = new ArrayList<>();
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// LayoutInflater.from(mContext);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                switch (msg.what) {
                    case 1:
                        imageView.setImageBitmap(bitmap);
                        break;

                    default:
                        break;
                }
                super.handleMessage(msg);
            }
        };

    }


    @Override
    public int getCount() {
        int size = 0;
        if (flowerInfo != null) {
            switch (type) {
                case TYPE_NORMAL:
                    size = flowerInfo.size();
                    break;
                case TYPE_CP_LIST:
                    size = (flowerInfo.size() > 4 ? 4 : flowerInfo.size());
                    break;
                case TYPE_CP_DES:
                    size = (flowerInfo.size() > 5 ? 5 : flowerInfo.size());
                    break;
            }
        } else {
            size = 0;
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case TYPE_NORMAL:
                    convertView = mInflater.inflate(R.layout.flower_rank, null);
                    break;
                case TYPE_CP_LIST:
                    convertView = mInflater.inflate(R.layout.flower_rank_cp_list, null);
                    break;
                case TYPE_CP_DES:
                    convertView = mInflater.inflate(R.layout.flower_rank_cp_des, null);
                    break;

            }
            holder.icon = (RoundImageView) convertView.findViewById(R.id.flower_icon);
            holder.count = (TextView) convertView.findViewById(R.id.flower_count);
            holder.medalImg = (RoundImageView) convertView.findViewById(R.id.flower_icon_medal);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == selectIndex) {
            convertView.setSelected(true);
        } else {
            convertView.setSelected(false);
        }
        if (position < 5) {
            Picasso.with(mContext).load(flowerInfo.get(position).getUser().getIcon()).into(holder.icon);
        } else {
            new Thread() {

                public void run() {
                    try {
                        imageView = holder.icon;
                        bitmap = ImageUtil.getImage(flowerInfo.get(position).getUser().getIcon());
                        handler.sendEmptyMessage(1);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }.start();
        }
        DiaobaoUtil.setMedalImgView(flowerInfo.get(position).getUser().getMedal(), holder.medalImg);

        if (flowerInfo.get(position).getNum() == null || ("").equals(flowerInfo.get(position).getNum())) //来自CP献花列表
        {
            holder.count.setVisibility(View.GONE);
        } else {
            holder.count.setVisibility(View.VISIBLE);
            holder.count.setText(flowerInfo.get(position).getNum());
        }
        if (is_text_show) {
            holder.count.setVisibility(View.VISIBLE);
            holder.count.setText(flowerInfo.get(position).getNum());
        } else {
            holder.count.setVisibility(View.GONE);
        }
        setOnClick(convertView, position);
        return convertView;
    }

    private void setOnClick(final View convertView, final int position) {
        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                SweepFlowerBean info = flowerInfo.get(position);
                String user_id = info.getUid();
                if (user_id == null) {
                    user_id = info.getUser().getUserId();
                }
                if (!user_id.equals(AccountService.getInstance().getUserUid())) {
                    UserDetailActivity.Companion.startActivity(mContext,user_id);
                }

            }
        });

    }

    private static class ViewHolder {
        private TextView count;
        private RoundImageView icon;
        private RoundImageView medalImg;
    }


    public void setFlowerInfo(List<SweepFlowerBean> flowerInfo) {

        if (this.flowerInfo != null) {
            this.flowerInfo.clear();
            this.flowerInfo.addAll(flowerInfo);
        }

        this.notifyDataSetChanged();

    }

}